package com.rocketmq.example;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

public class E05_SyncMessage {

    private static final String TOPIC = "BroadcastingMessageTopic";
    /**
     * lidecai
     * 同步消息，可以直接返回结果
     * 阻塞当前线程
     */
    public static void main(String[] args) {
        DefaultMQProducer producer = new DefaultMQProducer(TOPIC);
        producer.setNamesrvAddr(ExampleMQConfig.ENDPOINT.getName());
        try {
            producer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }
        try {
            for (int i = 0; i < 10; i++) {
                Message message = new Message(TOPIC, ExampleMQConfig.TAG.getName(),
                        ("message:" + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
                SendResult send = producer.send(message);
                System.err.println(send);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        producer.shutdown();
    }

}
